export const TOKEN_KEY = 'ApiAuth';
export const BACKURL_KEY = 'backurl_key';
export const INVID_KEY = 'invid_key';
export const APP = 'YQ_';

export const setToken = (token) => {
  localStorage.setItem(APP + TOKEN_KEY, token)
};
export const getToken = () => {
  const token = localStorage.getItem(APP + TOKEN_KEY);
  if (token && token !== '') {
    return token
  } else {
    //如果是开发环境，就自动写入测试用的token
    // if (process.env.NODE_ENV == 'development') {
    //   localStorage.setItem(APP + TOKEN_KEY,process.env.VUE_APP_DEBUG_TOKEN);
    //   return process.env.VUE_APP_DEBUG_TOKEN;
    // }
    return false
  }
};
export const getUserBackUrl = () => {
  const userBackUrl = sessionStorage.getItem(APP + BACKURL_KEY);
  if (userBackUrl && '' !== userBackUrl) {
    return userBackUrl
  } else {
    return false
  }
};
export const setUserBackUrl = (url) => {
  sessionStorage.setItem(APP + BACKURL_KEY, url)
};
export const getInvid = () => {
  const token = sessionStorage.getItem(APP + INVID_KEY);
  if (token) {
    return token
  } else {
    return false
  }
};
export const setInvid = (id) => {
  sessionStorage.setItem(APP + INVID_KEY, id)
};
export const getDateDiff = (str) => {
  let sDate1 = new Date();
  let Date2 = (new Date(str));
  //把相差的毫秒数转换为天数
  return Date2.getTime() - sDate1.getTime();  //返回相差毫秒数
};

export const GetQueryString = (name) => {
  let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  let r = window.location.search.substr(1).match(reg); //获取url中"?"符后的字符串并正则匹配
  let context = "";
  if (r != null)
    context = r[2];
  reg = null;
  r = null;
  return (context == null || context === "" || context === "undefined") ? false : context;
};
/**
 * 检测手机号
 * @param phoneNum 要检查的手机号
 * @returns boolen
 */
export const checkPhoneNum = (phoneNum) => {
  let validRule = /^(13[0-9]|14[5-9]|15[012356789]|166|17[0-8]|18[0-9]|19[8-9])[0-9]{8}$/;// 手机号码校验规则
  return validRule.test(phoneNum);
};
/**
 * 检测电话
 * @param tel 要检查的电话号
 * @returns boolen
 */
export const checkTel = (tel) => {
  return /^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$/.test(tel);
};
/**
 * 检测字符是否为空
 * @param str 要检查的
 * @returns boolen
 */
export const isNull = (str) => {
  return str === undefined || str === null || str === '';
};